/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servletpackage;
import datapackage.IntituleProf;
import datapackage.IntituleProfBean;
import datapackage.StaticClass;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author salim
 * role:
 * 1-recevoir le noenseig apres onclick
 * 2-avoir le numprof
 *
 * 3-les referencer au request beans pour la page clean page peut invoquer
 * la methode avoirMPpj pour afficher le conflit
 */
public class conflictServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException {
       if(StaticClass.debug)System.out.println("+++++++@ conflictservlet+++debut....................");

       String noens=request.getParameter("noenseig");
    if(noens !=null){
       int noenseig=Integer.parseInt(request.getParameter("noenseig"));
         
        if(noenseig !=0){
                 
       
          if(StaticClass.debug)System.out.println("+++++++@ conflictervlet+++noenseig"+noenseig);
          HttpSession session=request.getSession();
       
        
        
        IntituleProf ip=datapackage.DivertList.AvoirProf(noenseig) ;
        if(StaticClass.debug)System.out.println("+++++++@ conflictervlet+++le numprof :"+ip.getNumprof());
       // IntituleProfBean ipb=(IntituleProfBean) request.getAttribute("ipb");
       //definir l'intitule prof au request bean, elle contient le noenseig clicker

       IntituleProfBean ipb=(IntituleProfBean) session.getAttribute("ipb");
       ipb.setIp(ip);

        request.setAttribute("ipb",ipb);
        
        String address = "/cleanpage.jsp";

        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);

        
        
        
        
        
        
        
       }//fin de if not 0

         //if noenseig =0,on veut la definir au session pour la garder dans l'interface
         else{
              HttpSession session=request.getSession();
              IntituleProfBean ipb=(IntituleProfBean) session.getAttribute("ipb");
            //  IntituleProf ip=ipb.getIp();
            //   ip.setNoenseig(0);
             IntituleProf ip=datapackage.DivertList.AvoirProf(noenseig) ;//reourne une ip initializer a zero avec un noenseig=0
             ipb.setIp(ip);
              if(StaticClass.debug)System.out.println("+++++++@ conflictervlet+++noenseig="+ip.getNoenseig()+"ce la valeur initializer");
             request.setAttribute("ipb",ipb);

             String address = "/cleanpage.jsp";

        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
         }
      }//fin de if not null
    }//fin de la methode

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(conflictServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(conflictServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
